package com.aquarium.application;

import java.util.Collection;
import java.util.Iterator;

import com.aquarium.metier.Contient;
import com.aquarium.metier.ContientDAO;
import com.aquarium.metier.ContientDTO;

public class GererContientCtrl implements CtrlSkeleton<ContientDTO>{
	protected ContientDAO unContiDAO = new ContientDAO();
	protected Contient unConti = new Contient();
	protected ContientDTO unContiDTO = new ContientDTO();
	
    @Override
    public void createObj(ContientDTO Objet) throws NicheAPoissonsException,
                    HelperException {
            Iterator<Contient> it = null;
            boolean ct = false;
            Contient contient = null;
            int res = 0;
            unConti = HelperContient.toContient(Objet);
            it = unContiDAO.retreiveAllObjByAqua(unConti.getAqua_id()).iterator();
            while (it.hasNext() && !ct) {
                    contient = (Contient) it.next();
                    if(contient.getEspe_id() == unConti.getEspe_id()) ct = true;
            }
            if(ct){
                    res = unConti.getConti_nb_pois()+contient.getConti_nb_pois();
                    if(!(res <= 0)){
                            unConti.setConti_nb_pois(res);
                            unContiDAO.updateObj(unConti);
                    }else{
                            unContiDAO.deleteObj(unConti.getAqua_id(), unConti.getEspe_id());
                    }
                    
            }else{
                    unContiDAO.createObj(unConti);
            }
    }

	@Override
	public ContientDTO retreiveObj(int identifiant)
			throws NicheAPoissonsException, HelperException {
		unConti = unContiDAO.retreiveObj(identifiant);
		return HelperContient.toContientDTO(unConti);
	}

	@Override
	public void updateObj(ContientDTO Objet_a_modifier)
			throws NicheAPoissonsException, HelperException {
		unConti = HelperContient.toContient(Objet_a_modifier);
		unContiDAO.updateObj(unConti);
		
	}

	@Override
	public void deleteObj(int Identifiant) throws NicheAPoissonsException {
		unContiDAO.deleteObj(Identifiant);
		
	}
	
	public void deleteObj(int Identifiant, int id_espe) throws NicheAPoissonsException {
		unContiDAO.deleteObj(Identifiant, id_espe);
		
	}
	
	

	@Override
	public Collection<ContientDTO> retreiveAllObj()
			throws NicheAPoissonsException, HelperException {
		Collection<ContientDTO> uneListConti = HelperContient.toContientDTO(unContiDAO.retreiveAllObj());
		return uneListConti;
	}

	@Override
	public void deleteAllobj() throws NicheAPoissonsException {
		unContiDAO.deleteAllobj();
		
	}
	
	public Collection<ContientDTO> retreiveAllObjByIdAqua(Integer aqua_id)
			throws NicheAPoissonsException, HelperException {
		Collection<ContientDTO> uneListConti = HelperContient.toContientDTO(unContiDAO.retreiveAllObjByAqua(aqua_id));
		return uneListConti;
	}
	public Collection<ContientDTO> retreiveAllObjByIdEspe(Integer espe_id)
			throws NicheAPoissonsException, HelperException {
		Collection<ContientDTO> uneListConti = HelperContient.toContientDTO(unContiDAO.retreiveAllObjByEspe(espe_id));
		return uneListConti;
	}


}
